From: Iceyer Date: Sat, 29 Jul 2017 07:06:53 +0000 (+0800) Subject: Add DStandardPaths X-Git-Tag: archive/raspbian/5.7.12-2+rpi1^2~158^2~5 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=b4076e893be97b7c287cafac92cfcaa377445da1;p=dtkcore.git Add DStandardPaths Change-Id: I2aaab5098ead544880aa9055dea3217f8ca092d7 --- diff --git a/src/filesystem/DStandardPaths b/src/filesystem/DStandardPaths new file mode 100644 index 0000000..9d0b963 --- /dev/null +++ b/src/filesystem/DStandardPaths @@ -0,0 +1 @@ +#include "dstandardpaths.h" diff --git a/src/filesystem/dstandardpaths.cpp b/src/filesystem/dstandardpaths.cpp new file mode 100644 index 0000000..cbfca54 --- /dev/null +++ b/src/filesystem/dstandardpaths.cpp @@ -0,0 +1,97 @@ +/** + * Copyright (C) 2017 Deepin Technology Co., Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + **/ + +#include "dstandardpaths.h" + +#include + +DCORE_BEGIN_NAMESPACE + + +class DSnapStandardPaths +{ +public: + inline static QString writableLocation(QStandardPaths::StandardLocation /*type*/) + { + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + return env.value("SNAP_USER_COMMON"); + } + + inline static QStringList standardLocations(QStandardPaths::StandardLocation type) + { + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + + switch (type) { + case QStandardPaths::GenericDataLocation: { + QString snapRoot = env.value("SNAP"); + QString genericDataDir = snapRoot + "/usr/share/"; + return QStringList() << genericDataDir; + } + default: + break; + } + + return QStringList() << env.value("SNAP_USER_COMMON"); + } + +private: + DSnapStandardPaths(); + ~DSnapStandardPaths(); + Q_DISABLE_COPY(DSnapStandardPaths) +}; + + +static DStandardPaths::Mode s_mode = DStandardPaths::Auto; + +QString DStandardPaths::writableLocation(QStandardPaths::StandardLocation type) +{ + switch (s_mode) { + case Auto: + case Test: + return QStandardPaths::writableLocation(type); + case Snap: + return DSnapStandardPaths::writableLocation(type); + } + return QStandardPaths::writableLocation(type); +} + +QStringList DStandardPaths::standardLocations(QStandardPaths::StandardLocation type) +{ + switch (s_mode) { + case Auto: + case Test: + return QStandardPaths::standardLocations(type); + case Snap: + return DSnapStandardPaths::standardLocations(type); + } + return QStandardPaths::standardLocations(type); +} + +QString DStandardPaths::locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options) +{ + return QStandardPaths::locate(type, fileName, options); +} + +QStringList DStandardPaths::locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options) +{ + return QStandardPaths::locateAll(type, fileName, options); +} + +QString DStandardPaths::findExecutable(const QString &executableName, const QStringList &paths) +{ + return QStandardPaths::findExecutable(executableName, paths); +} + +void DStandardPaths::setMode(DStandardPaths::Mode mode) +{ + s_mode = mode; + QStandardPaths::setTestModeEnabled(mode == Test); +} + +DCORE_END_NAMESPACE diff --git a/src/filesystem/dstandardpaths.h b/src/filesystem/dstandardpaths.h new file mode 100644 index 0000000..740a647 --- /dev/null +++ b/src/filesystem/dstandardpaths.h @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2017 Deepin Technology Co., Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + **/ + +#ifndef DTK_CORE_FILESYSTEM_DSTANDARDPATHS_H +#define DTK_CORE_FILESYSTEM_DSTANDARDPATHS_H + +#include + +#include "dtkcore_global.h" + +DCORE_BEGIN_NAMESPACE + +class DStandardPathsPrivate; +class DStandardPaths +{ +public: + enum Mode { + Auto, + Snap, + Test, + }; + + static QString writableLocation(QStandardPaths::StandardLocation type); + static QStringList standardLocations(QStandardPaths::StandardLocation type); + + static QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = QStandardPaths::LocateFile); + static QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options = QStandardPaths::LocateFile); + static QString findExecutable(const QString &executableName, const QStringList &paths = QStringList()); + static void setMode(Mode mode); + +private: + DStandardPaths(); + ~DStandardPaths(); + Q_DISABLE_COPY(DStandardPaths) +}; + +DCORE_END_NAMESPACE + +#endif // DTK_CORE_FILESYSTEM_DSTANDARDPATHS_H diff --git a/src/filesystem/filesystem.pri b/src/filesystem/filesystem.pri index 4f90a6d..0b53c14 100644 --- a/src/filesystem/filesystem.pri +++ b/src/filesystem/filesystem.pri @@ -5,12 +5,14 @@ HEADERS += \ $$PWD/dfilesystemwatcher.h \ $$PWD/dfilewatcher.h \ $$PWD/dfilewatchermanager.h \ - $$PWD/dpathbuf.h + $$PWD/dpathbuf.h \ + $$PWD/dstandardpaths.h SOURCES += \ $$PWD/dbasefilewatcher.cpp \ $$PWD/dfilewatcher.cpp \ - $$PWD/dfilewatchermanager.cpp + $$PWD/dfilewatchermanager.cpp \ + $$PWD/dstandardpaths.cpp linux { @@ -30,4 +32,5 @@ includes.files += \ $$PWD/DBaseFileWatcher \ $$PWD/DFileSystemWatcher \ $$PWD/DFileWatcherManager \ - $$PWD/DPathBuf + $$PWD/DPathBuf \ + $$PWD/DStandardPaths